/**
XenSim simulates xenbal.
Copyright (C) 2006 Travis Vachon

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/
package xensim;

import java.util.PriorityQueue;

public class Simulator {
	private SimGlobals globals;
	private long runtime;
	
	public Simulator(long runtime, SimGlobals globals){
		this.globals = globals;
		this.runtime = runtime;
	}

	/**
	 * An endless loop of executing events
	 * @throws ConfigurationError 
	 *
	 */
	public void run() throws ConfigurationError{
		if(globals.eventQueue.peek() == null) return;
		
		while(globals.eventQueue.peek().executionTime <= runtime){
			Event nextEvent = globals.eventQueue.poll();
			
							
			assert globals.currentTime <= nextEvent.executionTime; //Time should move forward.
			//System.out.println(globals.nodes.get(0).loadList.size());
			//System.out.println("e:" +globals.nodes.get(0).loadList.getFirst().getStopTime());
			//System.out.println("b:" +globals.nodes.get(0).loadList.getLast().getStopTime());
			//System.out.println("t:" + globals.currentTime);
//			if (globals.currentTime > 20000){
//				System.out.println(globals.nodes.get(0).loadList.size());
//				for (Node.TimeDomainPair pair: globals.nodes.get(0).loadList){
//				System.out.println(pair.getStartTime() + " " + pair.getStopTime());
//				
//				}
//				System.exit(1);
//			}
			globals.currentTime = nextEvent.executionTime;
			nextEvent.execute();
			
			if(globals.eventQueue.peek() == null) break;
		}
		
	}
	
	public static void main(String[] args){
		
//		SimulationGlobals globals = new SimulationGlobals();
//		
//		Simulator simulator = new Simulator(60000 , globals);
//		
//		Node node = new Node(globals);
//		
//		Domain domain1 = new Domain("Domain 1",
//				64000000,
//				4012,
//				new OneFn(),
//				new OneFn(),
//				new OneFn(),
//				globals
//				);
//		
//		node.bootDomain(domain1);
//
//		Domain domain2 = new Domain("Domain 2",
//				64000000,
//				4012,
//				new OneFn(),
//				new OneFn(),
//				new OneFn(),
//				globals
//				);
//
//		node.bootDomain(domain2);
//		
//		Domain domain3 = new Domain("Domain 3",
//				64000000,
//				4012,
//				new OneFn(),
//				new SinFn(),
//				new OneFn(),
//				globals
//				);
//
//		node.bootDomain(domain3);
//		
//		simulator.run();
//		
//		System.out.println(domain1);
//		System.out.println(domain2);
//		System.out.println(domain3);
	}
	
}
